﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness;

namespace WebApplication.Admin
{
    public partial class ChiTietHoaDonTraList : System.Web.UI.Page
    {
        CHUCNANG objchucnang = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            KiemtraQUyenSuDung();
            if (!IsPostBack)
            {
                LoadHienThi();
                NHACUNGCAP obj = null;
               
            }
        }
        public void LoadHienThi()
        {
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            CHITIETHOADONTRARepository repository = new CHITIETHOADONTRARepository(new EFRepository<CHITIETHOADONTRA>(), iunitfwork);
            IQueryable<CHITIETHOADONTRA> objChitietHoadontra = repository.All();
            GrHoaDonTra.DataSource = objChitietHoadontra;
            GrHoaDonTra.DataBind();
        }

        protected void GrHoaDonTra_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex < 0) return;
            CHITIETHOADONTRA oBjhoadontra = ((CHITIETHOADONTRA)e.Row.DataItem);
            if (oBjhoadontra != null)
            {
                Label lbnhacc = ((Label)e.Row.FindControl("lbnhacc"));
                if (!string.IsNullOrEmpty(oBjhoadontra.HOADONTRA.MANCC))
                {
                    lbnhacc.Text = oBjhoadontra.HOADONTRA.NHACUNGCAP.TENNCC;
                }
                Label lbmathang = ((Label)e.Row.FindControl("lbmathang"));
                if (!string.IsNullOrEmpty(oBjhoadontra.MAMH))
                {
                    lbmathang.Text = oBjhoadontra.MATHANG.TENMH;
                }
                Label lbngaytra = ((Label)e.Row.FindControl("lbngaytra"));
                if (!string.IsNullOrEmpty(oBjhoadontra.HOADONTRA.NGAYTRA.ToString()))
                {
                    lbngaytra.Text = oBjhoadontra.HOADONTRA.NGAYTRA.ToString();
                }
                Label lbhdn = ((Label)e.Row.FindControl("lbhdn"));
                if (!string.IsNullOrEmpty(oBjhoadontra.HOADONTRA.MAHDN))
                {
                    lbhdn.Text = oBjhoadontra.HOADONTRA.MAHDN;
                }
            }
            LinkButton lbtnsua = ((LinkButton)e.Row.FindControl("lbtnsua"));
            LinkButton lbtnxoa = ((LinkButton)e.Row.FindControl("lbtnxoa"));
            lbtnsua.Visible = false;
            lbtnxoa.Visible = false;
            if (objchucnang != null)
            {
                if (lbtnsua != null)
                {
                    if (objchucnang.ISEDIT == true)
                    {
                        lbtnsua.Visible = true;
                    }
                    else
                    {
                        lbtnsua.Visible = false;
                    }
                }
                if (lbtnxoa != null)
                {
                    if (objchucnang.ISDELETE == true)
                    {
                        lbtnsua.Visible = true;
                    }
                    else
                    {
                        lbtnxoa.Visible = false;
                    }
                }
            }
        }

        protected void GrHoaDonTra_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "lbtnsua")
            {
                Response.Redirect("ChiTietHoaDonTraAdd.aspx?MAHDT=" + e.CommandArgument);
            }
            if (e.CommandName == "lbtnxoa")
            {
                try
                {
                    IUnitOfWork iunitfwork = new EFUnitOfWork();
                    CHITIETHOADONTRARepository repository = new CHITIETHOADONTRARepository(new EFRepository<CHITIETHOADONTRA>(), iunitfwork);
                    CHITIETHOADONTRA objHdtra = repository.GetById(e.CommandArgument.ToString());
                    repository.Delete(objHdtra);
                    repository.Save();
                    HOADONTRARepository repositoryHDtra = new HOADONTRARepository(new EFRepository<HOADONTRA>(), iunitfwork);
                    HOADONTRA objhdt = repositoryHDtra.GetById(e.CommandArgument.ToString());
                    repositoryHDtra.Delete(objhdt);
                    repositoryHDtra.Save();
                }
                catch
                {
                    RegisterClientScriptBlock("Thông báo", "<script> alert('Không thể xóa được')</script>");
                }
            }
        }
        public void KiemtraQUyenSuDung()
        {
            lbtnthemmoi.Visible = false;
            if (Session["MANV"] != null)
            {
                string strMANV = Session["MANV"].ToString();
                IUnitOfWork iunitwork = new EFUnitOfWork();
                lbtnthemmoi.Visible = false;
                NHANVIENRepository repository = new NHANVIENRepository(new EFRepository<NHANVIEN>(), iunitwork);
                NHANVIEN objNhanvien = repository.All().Where(p => p.MANV == strMANV).FirstOrDefault();
                if (objNhanvien != null)
                {
                    objchucnang = Commomcs.QuyenTruyCapChucnang(Request.Url.AbsolutePath, objNhanvien.MANV);
                    if (objchucnang != null)
                    {
                        if (objchucnang.ISADD == true)
                        {
                            lbtnthemmoi.Visible = true;
                        }
                        else
                        {
                            lbtnthemmoi.Visible = false;
                        }

                    }
                }
            }
        }
        protected void GrHoaDonTra_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}